package com.firebird.app.kotlin.stock.trend.bean

import android.graphics.*
import android.graphics.Paint.Align


/**
 * 绘制分时走势的坐标系
 * @author ljk
 * @date   2019年10月4日
 */
class FsCoordinateSystem(viewWidth: Int, viewHeight: Int) {
    var viewWidth: Int
    var viewHeight //分时走势视图的长，宽
            : Int
    private var heightFsBorder = 0
    private var heightFslBorder //分时线，分时量的边框高度
            = 0
    private var paint: Paint? = null
    private var canvas: Canvas? = null
    private val path: Path
    fun drawCoordinateSystem(canvas: Canvas, paint: Paint) {
        this.paint = paint
        this.canvas = canvas
        paint.color = Color.WHITE
        canvas.drawRGB(255, 255, 255) //绘制背景颜色
        paint.style = Paint.Style.STROKE
        paint.color = Color.LTGRAY
        paint.strokeWidth = 1f
        paint.pathEffect = null //设置虚线效果为null
        //绘制分时走势外边框
        heightFsBorder = viewHeight / 19 * 12
        canvas.drawRect(1f, 1f, viewWidth.toFloat(), heightFsBorder.toFloat(), paint)
        //绘制分时量外边框
        heightFslBorder = viewHeight / 19 * 13
        canvas.drawRect(1f, heightFslBorder.toFloat(), viewWidth.toFloat(), viewHeight.toFloat(), paint)
        drawXuXian()
        drawXData()
    }

    //画参考系中的虚线
    private fun drawXuXian() {
        paint!!.style = Paint.Style.STROKE
        paint!!.color = Color.LTGRAY
        paint!!.strokeWidth = 1f
        //绘制参考虚线
        val effect: PathEffect = DashPathEffect(floatArrayOf(2f, 4f), 1f)
        paint!!.pathEffect = effect
        //绘制虚线参考线 横线
        val margin = 5
        val heightFsXuXian = heightFsBorder - margin * 2
        path.moveTo(1f, heightFsXuXian / 4 * 0 + margin.toFloat())
        path.lineTo(viewWidth.toFloat(), heightFsXuXian / 4 * 0 + margin.toFloat())
        canvas!!.drawPath(path, paint) //第一条
        path.moveTo(1f, heightFsXuXian / 4 * 1 + margin.toFloat())
        path.lineTo(viewWidth.toFloat(), heightFsXuXian / 4 * 1 + margin.toFloat())
        canvas!!.drawPath(path, paint) //第二条
        path.moveTo(1f, heightFsXuXian / 4 * 2 + margin.toFloat())
        path.lineTo(viewWidth.toFloat(), heightFsXuXian / 4 * 2 + margin.toFloat())
        canvas!!.drawPath(path, paint) //第三条
        path.moveTo(1f, heightFsXuXian / 4 * 3 + margin.toFloat())
        path.lineTo(viewWidth.toFloat(), heightFsXuXian / 4 * 3 + margin.toFloat())
        canvas!!.drawPath(path, paint) //第四条
        path.moveTo(1f, heightFsXuXian / 4 * 4 + margin.toFloat())
        path.lineTo(viewWidth.toFloat(), heightFsXuXian / 4 * 4 + margin.toFloat())
        canvas!!.drawPath(path, paint) //第五条
        path.moveTo(1f, viewHeight / 19 * 16 + 5.toFloat())
        path.lineTo(viewWidth.toFloat(), viewHeight / 19 * 16 + margin.toFloat())
        canvas!!.drawPath(path, paint) //第六条
        //绘制虚线参考线 竖线
        val widthXuXian = viewWidth
        path.moveTo(widthXuXian / 4 * 1.toFloat(), margin.toFloat())
        path.lineTo(widthXuXian / 4 * 1.toFloat(), heightFsBorder - margin.toFloat())
        canvas!!.drawPath(path, paint) //第一条
        path.moveTo(widthXuXian / 4 * 2.toFloat(), margin.toFloat())
        path.lineTo(widthXuXian / 4 * 2.toFloat(), heightFsBorder - margin.toFloat())
        canvas!!.drawPath(path, paint) //第二条
        path.moveTo(widthXuXian / 4 * 3.toFloat(), margin.toFloat())
        path.lineTo(widthXuXian / 4 * 3.toFloat(), heightFsBorder - margin.toFloat())
        canvas!!.drawPath(path, paint) //第三条
        path.moveTo(widthXuXian / 4 * 1.toFloat(), heightFslBorder.toFloat())
        path.lineTo(widthXuXian / 4 * 1.toFloat(), viewHeight.toFloat())
        canvas!!.drawPath(path, paint) //第四条
        path.moveTo(widthXuXian / 4 * 2.toFloat(), heightFslBorder.toFloat())
        path.lineTo(widthXuXian / 4 * 2.toFloat(), viewHeight.toFloat())
        canvas!!.drawPath(path, paint) //第五条
        path.moveTo(widthXuXian / 4 * 3.toFloat(), heightFslBorder.toFloat())
        path.lineTo(widthXuXian / 4 * 3.toFloat(), viewHeight.toFloat())
        canvas!!.drawPath(path, paint) //第六条
    }

    //绘制X轴数据
    private fun drawXData() {
        paint!!.color = Color.DKGRAY
        paint!!.style = Paint.Style.FILL
        paint!!.pathEffect = null
        paint!!.strokeWidth = 3f
        paint!!.textSize = 14f
        paint!!.typeface = Typeface.DEFAULT_BOLD
        paint!!.textAlign = Align.LEFT
        canvas!!.drawText("09:30", 1f, heightFsBorder + 15.toFloat(), paint)
        paint!!.textAlign = Align.CENTER
        canvas!!.drawText("11:30", viewWidth / 2.toFloat(), heightFsBorder + 15.toFloat(), paint)
        paint!!.textAlign = Align.RIGHT
        canvas!!.drawText("15:00", viewWidth.toFloat(), heightFsBorder + 15.toFloat(), paint)
    }

    private fun drawYData() {}

    init {
        path = Path()
        this.viewWidth = viewWidth - 1
        this.viewHeight = viewHeight - 1
    }
}
